﻿using Mix.Constant.Enums;
using Mix.Database.Entities.Cms;
using Mix.Heart.Extensions;
using Mix.Heart.UnitOfWork;
using Mix.Heart.ViewModel;
using Mix.Shared.Helpers;
using System.ComponentModel.DataAnnotations;

namespace Mix.RepoDb.ViewModels
{
    public sealed class MixDbTableRelationshipViewModel
        : ViewModelBase<MixCmsContext, MixDbTableRelationship, int, MixDbTableRelationshipViewModel>
    {
        #region Properties
        public int ParentId { get; set; }
        public int ChildId { get; set; }
        public string DisplayName { get; set; }
        public string PropertyName { get; set; }
        public string SourceTableName { get; set; }
        public string DestinateTableName { get; set; }
        public string SourceColumnName { get; set; }
        public string? DestinateColumnName { get; set; }
        public MixDbTableRelationshipType Type { get; set; }
        #endregion

        #region Constructors

        public MixDbTableRelationshipViewModel()
        {

        }

        public MixDbTableRelationshipViewModel(UnitOfWorkInfo unitOfWorkInfo) : base(unitOfWorkInfo)
        {
        }

        public MixDbTableRelationshipViewModel(MixDbTableRelationship entity, UnitOfWorkInfo? uowInfo = null)
            : base(entity, uowInfo)
        {
        }

        #endregion

        #region Overrides
        public override async Task Validate(CancellationToken cancellationToken)
        {

            if (Repository.Table.Any(m => !m.Id.Equals(Id) && m.ParentId.Equals(ParentId) && m.ChildId.Equals(ChildId)))
            {
                IsValid = false;
                Errors.Add(new ValidationResult("Entity Existed"));
            }
            if (MixHelper.IsDefaultId(ParentId))
            {
                IsValid = false;
                Errors.Add(new("Parent Id cannot be null"));
            }
            if (MixHelper.IsDefaultId(ChildId))
            {
                IsValid = false;
                Errors.Add(new("Child Id cannot be null"));
            }
            if (ParentId == 0 || ChildId == 0)
            {
                IsValid = false;
                Errors.Add(new($"Ivalid relationship: parent Id = {ParentId} - child Id = {ChildId} - Type = {Type}"));
            }

            await base.Validate(cancellationToken);
        }
        #endregion
    }
}
